import copy
from collections import defaultdict
from functools import lru_cache

n = int(input())
nums = [0] + list(map(int, input().split()))
edges = defaultdict(list)
for _ in range(n-1):
    u, v = map(int, input().split())
    edges[u].append(v)

@lru_cache(None)
def dfs(node):
    # 该节点能得到的最大值
    if not edges[node]: return {nums[node]}
    values = {0}
    for child in edges[node]:
        new_copy = {0}
        for val in values:
            for v in dfs(child):
                new_copy.add(val + v)
        values.update(new_copy)

    for val in copy.copy(values):
        if val > nums[node]:
            values.remove(val)
    return values

print(max(dfs(1)))

